package com.zuofei.springcloud.task;

import com.zuofei.springcloud.job.FileJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

public class BaseTask implements Job {
    @Autowired
    private Scheduler scheduler;

    public String getCronExpression() {
        return null;
    }


    @PostConstruct
    public void init() {
        JobDetail jobDetail = JobBuilder.newJob(this.getClass()).withIdentity(this.getClass().getSimpleName()+"_job",this.getClass().getSimpleName()+"_group").build();
//        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
//        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group").startNow().withSchedule(simpleScheduleBuilder).build();
        String cronExpression = getCronExpression();
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(this.getClass().getSimpleName()+"_job", this.getClass().getSimpleName()+"_group").withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();

        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //这里并没有政治意义上的实现
    }
}
